home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / view.el < prev    next >
Lisp/Scheme  |  1993-06-03  |  17KB  |  442 lines

  1. ;;; view.el --- peruse file or buffer without editing.
  2.  
  3. ;; Copyright (C) 1985, 1989 Free Software Foundation, Inc.
  4.  
  5. ;; Author: K. Shane Hartman
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This package provides the `view' major mode documented in the Emacs
  27. ;; user's manual.
  28.  
  29. ;;; Code:
  30.  
  31. (defvar view-mode-map nil)
  32. (if view-mode-map
  33.     nil
  34.   (setq view-mode-map (make-keymap))
  35.   (fillarray (nth 1 view-mode-map) 'View-undefined)
  36.   (define-key view-mode-map "\C-c" 'view-exit)
  37.   (define-key view-mode-map "\C-z" 'suspend-emacs)
  38.   (define-key view-mode-map "q" 'view-exit)
  39.   (define-key view-mode-map "-" 'negative-argument)
  40.   (define-key view-mode-map "0" 'digit-argument)
  41.   (define-key view-mode-map "1" 'digit-argument)
  42.   (define-key view-mode-map "2" 'digit-argument)
  43.   (define-key view-mode-map "3" 'digit-argument)
  44.   (define-key view-mode-map "4" 'digit-argument)
  45.   (define-key view-mode-map "5" 'digit-argument)
  46.   (define-key view-mode-map "6" 'digit-argument)
  47.   (define-key view-mode-map "7" 'digit-argument)
  48.   (define-key view-mode-map "8" 'digit-argument)
  49.   (define-key view-mode-map "9" 'digit-argument)
  50.   (define-key view-mode-map "\C-u" 'universal-argument)
  51.   (define-key view-mode-map "\e" nil)
  52.   (define-key view-mode-map "\C-x" 'Control-X-prefix)
  53.   (define-key view-mode-map "\e-" 'negative-argument)
  54.   (define-key view-mode-map "\e0" 'digit-argument)
  55.   (define-key view-mode-map "\e1" 'digit-argument)
  56.   (define-key view-mode-map "\e2" 'digit-argument)
  57.   (define-key view-mode-map "\e3" 'digit-argument)
  58.   (define-key view-mode-map "\e4" 'digit-argument)
  59.   (define-key view-mode-map "\e5" 'digit-argument)
  60.   (define-key view-mode-map "\e6" 'digit-argument)
  61.   (define-key view-mode-map "\e7" 'digit-argument)
  62.   (define-key view-mode-map "\e8" 'digit-argument)
  63.   (define-key view-mode-map "\e9" 'digit-argument)
  64.   (define-key view-mode-map "<" 'beginning-of-buffer)
  65.   (define-key view-mode-map ">" 'end-of-buffer)
  66.   (define-key view-mode-map "\ev" 'View-scroll-lines-backward)
  67.   (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
  68.   (define-key view-mode-map " " 'View-scroll-lines-forward)
  69.   (define-key view-mode-map "\177" 'View-scroll-lines-backward)
  70.   (define-key view-mode-map "\n" 'View-scroll-one-more-line)
  71.   (define-key view-mode-map "\r" 'View-scroll-one-more-line)
  72.   (define-key view-mode-map "\C-l" 'recenter)
  73.   (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
  74.   (define-key view-mode-map "g" 'View-goto-line)
  75.   (define-key view-mode-map "=" 'what-line)
  76.   (define-key view-mode-map "." 'set-mark-command)
  77.   (define-key view-mode-map "\C-@" 'set-mark-command)
  78.   (define-key view-mode-map "'" 'View-back-to-mark)
  79.   (define-key view-mode-map "@" 'View-back-to-mark)  
  80.   (define-key view-mode-map "x" 'exchange-point-and-mark)
  81.   (define-key view-mode-map "h" 'Helper-describe-bindings)
  82.   (define-key view-mode-map "?" 'Helper-describe-bindings)
  83.   (define-key view-mode-map (char-to-string help-char) 'Helper-help)
  84.   (define-key view-mode-map "\C-n" 'next-line)
  85.   (define-key view-mode-map "\C-p" 'previous-line)
  86.   (define-key view-mode-map "\C-s" 'isearch-forward)
  87.   (define-key view-mode-map "\C-r" 'isearch-backward)
  88.   (define-key view-mode-map "s" 'isearch-forward)
  89.   (define-key view-mode-map "r" 'isearch-backward)
  90.   (define-key view-mode-map "/" 'View-search-regexp-forward)
  91.   (define-key view-mode-map "\\" 'View-search-regexp-backward)
  92.   ;; This conflicts with the standard binding of isearch-regexp-forward
  93.   (define-key view-mode-map "\e\C-s" 'View-search-regexp-forward)
  94.   (define-key view-mode-map "\e\C-r" 'View-search-regexp-backward)  
  95.   (define-key view-mode-map "n" 'View-search-last-regexp-forward)
  96.   (define-key view-mode-map "p" 'View-search-last-regexp-backward)
  97.   )
  98.  
  99.  
  100. ;;;###autoload
  101. (defun view-file (file-name)
  102.   "View FILE in View mode, returning to previous buffer when done.
  103. The usual Emacs commands are not available; instead,
  104. a special set of commands (mostly letters and punctuation)
  105. are defined for moving around in the buffer.
  106. Space scrolls forward, Delete scrolls backward.
  107. For list of all View commands, type ? or h while viewing.
  108.  
  109. Calls the value of  view-hook  if that is non-nil."
  110.   (interactive "fView file: ")
  111.   (let ((old-buf (current-buffer))
  112.     (had-a-buf (get-file-buffer file-name))
  113.     (buf-to-view (find-file-noselect file-name)))
  114.     (switch-to-buffer buf-to-view t)
  115.     (view-mode old-buf
  116.            (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  117.             'kill-buffer))))
  118.  
  119. ;;;###autoload
  120. (defun view-file-other-window (file-name)
  121.   "View FILE in View mode in other window.
  122. Return to previous buffer when done.
  123. The usual Emacs commands are not available; instead,
  124. a special set of commands (mostly letters and punctuation)
  125. are defined for moving around in the buffer.
  126. Space scrolls forward, Delete scrolls backward.
  127. For list of all View commands, type ? or h while viewing.
  128.  
  129. Calls the value of  view-hook  if that is non-nil."
  130.   (interactive "fView file: ")
  131.   (let ((old-arrangement (current-window-configuration))
  132.     (had-a-buf (get-file-buffer file-name))
  133.     (buf-to-view (find-file-noselect file-name)))
  134.     (switch-to-buffer-other-window buf-to-view)
  135.     (view-mode old-arrangement
  136.            (and (not had-a-buf) (not (buffer-modified-p buf-to-view))
  137.             'kill-buffer))))
  138.  
  139. ;;;###autoload
  140. (defun view-buffer (buffer-name)
  141.   "View BUFFER in View mode, returning to previous buffer when done.
  142. The usual Emacs commands are not available; instead,
  143. a special set of commands (mostly letters and punctuation)
  144. are defined for moving around in the buffer.
  145. Space scrolls forward, Delete scrolls backward.
  146. For list of all View commands, type ? or h while viewing.
  147.  
  148. Calls the value of  view-hook  if that is non-nil."
  149.   (interactive "bView buffer: ")
  150.   (let ((old-buf (current-buffer)))
  151.     (switch-to-buffer buffer-name t)
  152.     (view-mode old-buf nil)))
  153.  
  154. ;;;###autoload
  155. (defun view-buffer-other-window (buffer-name not-return)
  156.   "View BUFFER in View mode in another window,
  157. returning to original buffer when done  ONLY if 
  158. prefix argument not-return is nil (as by default).
  159.   The usual Emacs commands are not available; instead,
  160. a special set of commands (mostly letters and punctuation)
  161. are defined for moving around in the buffer.
  162. Space scrolls forward, Delete scrolls backward.
  163. For list of all View commands, type ? or h while viewing.
  164.  
  165. Calls the value of  view-hook  if that is non-nil."
  166.   (interactive "bView buffer:\nP")
  167.   (let ((return-to (and not-return (current-window-configuration))))
  168.     (switch-to-buffer-other-window buffer-name)
  169.     (view-mode return-to)))
  170.  
  171. ;;;###autoload
  172. (defun view-mode (&optional prev-buffer action)
  173.   "Major mode for viewing text but not editing it.
  174. Letters do not insert themselves.  Instead these commands are provided.
  175. Most commands take prefix arguments.  Commands dealing with lines
  176. default to \"scroll size\" lines (initially size of window).
  177. Search commands default to a repeat count of one.
  178. M-< or <    move to beginning of buffer.
  179. M-> or >    move to end of buffer.
  180. C-v or Space    scroll forward lines.
  181. M-v or DEL    scroll backward lines.
  182. CR or LF    scroll forward one line (backward with prefix argument).
  183. z        like Space except set number of lines for further
  184.            scrolling commands to scroll by.
  185. C-u and Digits    provide prefix arguments.  `-' denotes negative argument.
  186. =        prints the current line number.
  187. g        goes to line given by prefix argument.
  188. / or M-C-s    searches forward for regular expression
  189. \\ or M-C-r    searches backward for regular expression.
  190. n        searches forward for last regular expression.
  191. p        searches backward for last regular expression.
  192. C-@ or .    set the mark.
  193. x        exchanges point and mark.
  194. C-s or s    do forward incremental search.
  195. C-r or r    do reverse incremental search.
  196. @ or '        return to mark and pops mark ring.
  197.           Mark ring is pushed at start of every
  198.           successful search and when jump to line to occurs.
  199.           The mark is set on jump to buffer start or end.
  200. ? or h        provide help message (list of commands).
  201. \\[Helper-help]        provides help (list of commands or description of a command).
  202. C-n        moves down lines vertically.
  203. C-p        moves upward lines vertically.
  204. C-l        recenters the screen.
  205. q or C-c    exit view-mode and return to previous buffer.
  206.  
  207. Entry to this mode calls the value of  view-hook  if non-nil.
  208. \\{view-mode-map}"
  209. ;  Not interactive because dangerous things happen
  210. ;  if you call it without passing a buffer as argument
  211. ;  and they are not easy to fix.
  212. ;  (interactive)
  213.   (make-local-variable 'view-old-mode-line-buffer-identification)
  214.   (setq view-old-mode-line-buffer-identification
  215.     mode-line-buffer-identification)
  216.   (make-local-variable 'view-old-buffer-read-only)
  217.   (setq view-old-buffer-read-only buffer-read-only)
  218.   (make-local-variable 'view-old-mode-name)
  219.   (setq view-old-mode-name mode-name)
  220.   (make-local-variable 'view-old-major-mode)
  221.   (setq view-old-major-mode major-mode)
  222.   (make-local-variable 'view-old-local-map)
  223.   (setq view-old-local-map (current-local-map))
  224.   (make-local-variable 'view-old-Helper-return-blurb)
  225.   (setq view-old-Helper-return-blurb
  226.     (and (boundp 'Helper-return-blurb) Helper-return-blurb))
  227.  
  228.   (setq buffer-read-only t)
  229.   (setq mode-line-buffer-identification
  230.     (list
  231.      (if (buffer-file-name)
  232.          "Viewing %f"
  233.        "Viewing %b")))
  234.   (setq mode-name "View")
  235.   (setq major-mode 'view-mode)
  236.   (setq Helper-return-blurb
  237.     (format "continue viewing %s"
  238.         (if (buffer-file-name)
  239.             (file-name-nondirectory (buffer-file-name))
  240.             (buffer-name))))
  241.  
  242.   (make-local-variable 'view-exit-action)
  243.   (setq view-exit-action action)
  244.   (make-local-variable 'view-return-here)
  245.   (setq view-return-here prev-buffer)
  246.   (make-local-variable 'view-exit-position)
  247.   (setq view-exit-position (point-marker))
  248.  
  249.   (make-local-variable 'view-scroll-size)
  250.   (setq view-scroll-size nil)
  251.   (make-local-variable 'view-last-regexp)
  252.   (setq view-last-regexp nil)
  253.  
  254.   (beginning-of-line)
  255.   (setq goal-column nil)
  256.  
  257.   (use-local-map view-mode-map)
  258.   (run-hooks 'view-hook)
  259.   (view-helpful-message))
  260.  
  261.  
  262. (defun view-exit ()
  263.   "Exit from view-mode.
  264. If you viewed an existing buffer, that buffer returns to its previous mode.
  265. If you viewed a file that was not present in Emacs, its buffer is killed."
  266.   (interactive)
  267.   (setq mode-line-buffer-identification
  268.     view-old-mode-line-buffer-identification)
  269.   (setq major-mode view-old-major-mode)
  270.   (setq mode-name view-old-mode-name)
  271.   (use-local-map view-old-local-map)
  272.   (setq buffer-read-only view-old-buffer-read-only)
  273.  
  274.   (goto-char view-exit-position)
  275.   (set-marker view-exit-position nil)
  276.  
  277.   ;; Now do something to the buffer that we were viewing
  278.   ;; (such as kill it).
  279.   (let ((viewed-buffer (current-buffer))
  280.     (action view-exit-action))
  281.     (cond
  282.      ((bufferp view-return-here)
  283.       (switch-to-buffer view-return-here))
  284.      ((window-configuration-p view-return-here)
  285.       (set-window-configuration view-return-here)))
  286.     (if action (funcall action viewed-buffer))))
  287.  
  288. (defun view-helpful-message ()
  289.   (message
  290.      (substitute-command-keys
  291.       "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[view-exit] to quit.")))
  292.  
  293. (defun View-undefined ()
  294.   (interactive)
  295.   (ding)
  296.   (view-helpful-message))
  297.  
  298. (defun view-window-size () (1- (window-height)))
  299.  
  300. (defun view-scroll-size ()
  301.   (min (view-window-size) (or view-scroll-size (view-window-size))))
  302.  
  303. (defvar view-hook nil
  304.   "If non-nil, its value is called when viewing buffer or file.")
  305.  
  306. ;(defun view-last-command (&optional who what)
  307. ;  (setq view-last-command-entry this-command)
  308. ;  (setq view-last-command who)
  309. ;  (setq view-last-command-argument what))
  310.  
  311. ;(defun View-repeat-last-command ()
  312. ;  "Repeat last command issued in View mode."
  313. ;  (interactive)
  314. ;  (if (and view-last-command
  315. ;       (eq view-last-command-entry last-command))
  316. ;      (funcall view-last-command view-last-command-argument))
  317. ;  (setq this-command view-last-command-entry))
  318.  
  319. (defun View-goto-line (&optional line)
  320.   "Move to LINE in View mode.
  321. Display is centered at LINE.  Sets mark at starting position and pushes
  322. mark ring."
  323.   (interactive "p")
  324.   (push-mark)
  325.   (goto-line (or line 1))
  326.   (recenter (/ (view-window-size) 2)))
  327.  
  328. (defun View-scroll-lines-forward (&optional lines)
  329.   "Scroll forward in View mode, or exit if end of text is visible.
  330. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  331. Arg is number of lines to scroll."
  332.   (interactive "P")
  333.   (if (pos-visible-in-window-p (point-max))
  334.       (view-exit)
  335.     (setq lines
  336.       (if lines (prefix-numeric-value lines)
  337.         (view-scroll-size)))
  338.     ;; (view-last-command 'View-scroll-lines-forward lines)
  339.     (if (>= lines (view-window-size))
  340.     (scroll-up nil)
  341.       (if (>= (- lines) (view-window-size))
  342.       (scroll-down nil)
  343.     (scroll-up lines)))
  344.     (cond ((pos-visible-in-window-p (point-max))
  345.        (goto-char (point-max))
  346.        (recenter -1)
  347.        (message (substitute-command-keys
  348.              "End.  Type \\[view-exit] to quit viewing."))))
  349.     (move-to-window-line -1)
  350.     (beginning-of-line)))
  351.  
  352. (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
  353.   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
  354. This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
  355. The absolute value of LINES is used, so this command can be used to scroll
  356. backwards (but \"scroll size\" is always positive).  If LINES is greater than
  357. window height or omitted, then window height is assumed.  If LINES is less
  358. than window height then scrolling context is provided from previous screen."
  359.   (interactive "P")
  360.   (if (not lines)
  361.       (setq view-scroll-size (view-window-size))
  362.     (setq lines (prefix-numeric-value lines))
  363.     (setq view-scroll-size
  364.       (min (if (> lines 0) lines (- lines)) (view-window-size))))
  365.   (View-scroll-lines-forward lines))
  366.  
  367. (defun View-scroll-one-more-line (&optional arg)
  368.   "Scroll one more line up in View mode.
  369. With ARG scroll one line down."
  370.   (interactive "P")
  371.   (View-scroll-lines-forward (if (not arg) 1 -1)))
  372.  
  373. (defun View-scroll-lines-backward (&optional lines)
  374.   "Scroll backward in View mode.
  375. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  376. Arg is number of lines to scroll."
  377.   (interactive "P")
  378.   (View-scroll-lines-forward (if lines
  379.                  (- (prefix-numeric-value lines))
  380.                    (- (view-scroll-size)))))
  381.   
  382. (defun View-search-regexp-forward (times regexp)
  383.   "Search forward for NTH occurrence of REGEXP in View mode.
  384. Displays line found at center of window.  REGEXP is remembered for
  385. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  386.   (interactive "p\nsSearch forward (regexp): ")
  387.   (if (> (length regexp) 0)
  388.       (progn
  389.        ;(view-last-command 'View-search-last-regexp-forward times)
  390.     (view-search times regexp))))
  391.  
  392. (defun View-search-regexp-backward (times regexp)
  393.   "Search backward from window start for NTH instance of REGEXP in View mode.
  394. Displays line found at center of window.  REGEXP is remembered for
  395. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  396.   (interactive "p\nsSearch backward (regexp): ")
  397.   (View-search-regexp-forward (- times) regexp))
  398.  
  399. (defun View-search-last-regexp-forward (times)
  400.   "Search forward from window end for NTH instance of last regexp in View mode.
  401. Displays line found at center of window.  Sets mark at starting position
  402. and pushes mark ring."
  403.   (interactive "p")
  404.   (View-search-regexp-forward times view-last-regexp))
  405.  
  406. (defun View-search-last-regexp-backward (times)
  407.   "Search backward from window start for NTH instance of last regexp in View mode.
  408. Displays line found at center of window.  Sets mark at starting position and
  409. pushes mark ring."
  410.   (interactive "p")
  411.   (View-search-regexp-backward times view-last-regexp))
  412.  
  413. (defun View-back-to-mark (&optional ignore)
  414.   "Return to last mark set in View mode, else beginning of file.
  415. Displays line at center of window.  Pops mark ring so successive
  416. invocations return to earlier marks."
  417.   (interactive)
  418.   (goto-char (or (mark) (point-min)))
  419.   (pop-mark)
  420.   (recenter (/ (view-window-size) 2)))
  421.          
  422. (defun view-search (times regexp)
  423.   (setq view-last-regexp regexp)
  424.   (let (where)
  425.     (save-excursion
  426.       (move-to-window-line (if (< times 0) 0 -1))
  427.       (if (re-search-forward regexp nil t times)
  428.       (setq where (point))))
  429.     (if where
  430.     (progn
  431.       (push-mark)
  432.       (goto-char where)
  433.       (beginning-of-line)
  434.       (recenter (/ (view-window-size) 2)))
  435.       (message "Can't find occurrence %d of %s" times regexp)
  436.       (sit-for 4))))
  437.  
  438.  
  439. (provide 'view)
  440.  
  441. ;;; view.el ends here
  442.